<%
   'Programed By DarkFlash
%>
<%
	Option Explicit

	Response.Expires = -1
	Response.Buffer = true
	Server.ScriptTimeOut = 2*60*60
	Response.flush
%>
<HTML>
<HEAD>
<!-- #include file="adminFunc.asp" -->
<%fnAdminCheck%>
<!-- #include file="fileFunc.asp" -->
<%
	Dim mode,defaultPath
	Dim strAttach
	
	mode = Request("mode")
	defaultPath = Request("defaultPath")
	
	SELECT CASE mode
		CASE "save"
			fnSaveAttach
		CASE "del"
			fnDeleteAttach
	END SELECT
	
	Function fnSaveAttach()
		Dim UploadForm,Fso
		Dim i,bFolder,Fd
		Dim lngError
		
		on Error Resume Next
		
		defaultPath = defaultPath & Date & "/"
		
		Set Fso = Server.CreateObject("Scripting.FileSystemObject")
		
		err.Clear
		If Not Fso.FolderExists(Server.MapPath(defaultPath)) Then Fso.CreateFolder Server.MapPath(defaultPath)
		If err <> 0 Then
			mode = ""
			Set fso = nothing
			Exit Function
		End if
		
		Set UploadForm = Server.CreateObject("DEXT.fileUpload")
		
		If Fso.FileExists(Server.MapPath(defaultPath & UploadForm("inputAttach").fileName)) Then
			i = 0
			bFolder = false
			Do While Fso.FolderExists(Server.MapPath(defaultPath) & i)
				If Not Fso.FileExists(Server.MapPath(defaultPath & i & "/" & UploadForm("inputAttach").fileName)) Then
					bFolder = true
					Exit Do
				End if
				
				i = i + 1
			Loop
			defaultPath = defaultPath & i & "/"
			
			err.Clear 
			If Not bFolder Then Fso.CreateFolder Server.MapPath(defaultPath)
			If err <> 0 Then
				mode = ""
				Set UploadForm = nothing
				Set Fso = nothing
				Exit Function
			End if
		End if
		
		strAttach = fnFileSave(UploadForm,"","inputAttach",defaultPath,4,lngError)
		
		Set UploadForm = nothing
		Set Fso = nothing
	End Function
	
	Function fnDeleteAttach()
		Dim Fso
		Dim arrAttach,Item
		on Error Resume Next
		
		arrAttach = Split(Request("attachFile"),", ")
		
		If UBound(arrAttach) >= 0 Then
			Set Fso = Server.CreateObject("Scripting.FileSystemObject")
				For Each Item In arrAttach
					If Fso.FileExists(Server.MapPath(Item)) Then Fso.DeleteFile Server.MapPath(Item)
					If Fso.GetFolder(Fso.GetParentFolderName(Server.MapPath(Item))).files.Count < 1 AND _
						Fso.GetFolder(Fso.GetParentFolderName(Server.MapPath(Item))).subFolders.Count < 1 Then Fso.DeleteFolder Fso.GetParentFolderName(server.MapPath(Item))
				Next
			Set Fso = nothing
		End if
	End Function
%>
<script language="JavaScript">
<!--
parent.getAttach("<%=mode%>","<%=defaultPath%>","<%=strAttach%>");
//-->
</script>